let app = getApp();
Page({
    data: {
       header:{},
       userInfo:{}, 
       title:'',
       content:'',
       changeId:null,
       linshiArr:[],
       "province": "湖北省",
       "city": "请选择",
       item:'武汉',
       choosetype:1
   },
    turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=02'
      })
    },
    onLoad: function(options) {
    console.log('*****===12312')
    if (!options.id&&wx.getStorageSync('typeIdl')) {
       console.log('清空缓存')
        wx.setStorageSync('typel1',null);
                wx.setStorageSync('typel2',null);
                wx.setStorageSync('typel3',null);
                wx.setStorageSync('typel4',null);
                wx.setStorageSync('typel5',null);
                wx.setStorageSync('typel6',null);
                wx.setStorageSync('typel7',null);
                wx.setStorageSync('typel8',null);
                wx.setStorageSync('typel9',null);
                wx.setStorageSync('typel10',null);
                wx.setStorageSync('typel11',null);
                wx.setStorageSync('typeIdl',null);

    }
     var address= wx.getStorageSync('address');
        this.setData({
            address:address,
            city:address.city,
            item:address.city,
            province:address.province
      })
     this.setData({
          type:options.type,
          id:options.id
     })  

     getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  
       if (this.data.id) {
        this.setData({
          changeId:this.data.id
        })
         this.getDetail(this.data.id);
       }
       
     })
    },
     getDetail(id){
        var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl+'/wx/yxj/equipment/detail?id='+id,
            method:"GET",
            header:  this.data.header,
            success: function(res) {
              console.log("**********res.data.obj")
              console.log(res.data.obj)
               wx.setStorageSync('typel1',res.data.obj.itemList[0]); 
               wx.setStorageSync('typel2',res.data.obj.itemList[1]); 
               wx.setStorageSync('typel3',res.data.obj.itemList[2]); 
               wx.setStorageSync('typel4',res.data.obj.itemList[3]); 
               wx.setStorageSync('typel5',res.data.obj.itemList[4]); 
               wx.setStorageSync('typel6',res.data.obj.itemList[5]); 
               wx.setStorageSync('typel7',res.data.obj.itemList[6]); 
               wx.setStorageSync('typel8',res.data.obj.itemList[7]); 
               wx.setStorageSync('typel9',res.data.obj.itemList[8]); 
               wx.setStorageSync('typel10',res.data.obj.itemList[9]); 
               wx.setStorageSync('typel11',res.data.obj.itemList[10]); 
               wx.setStorageSync('chooseProvince02',res.data.obj.province);
               wx.setStorageSync('chooseCity02',res.data.obj.city);
                // price:this.data.price,
                // butlerMsg:this.data.butlerMsg,
                // sellerMsg:this.data.sellerMsg,
                // url:this.data.linshiArr[0].url,
                // isTestMachine:this.data.choosetype,
                // province:this.data.province,
                // city:this.data.city,

                _self.setData({
                   price:res.data.obj.price,
                   butlerMsg:res.data.obj.butlerMsg,
                   sellerMsg:res.data.obj.sellerMsg,
                   linshiArr:[{url:res.data.obj.url}],
                   choosetype:res.data.obj.isTestMachine,
                   province:res.data.obj.province,
                   city:res.data.obj.city,

                })


               _self.onShow();
              
           
           
         }
       })
    },
    getNoReady:function(){
        
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成
    },
     onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1]
      })
    },
    onShow: function() {
     
       var chooseProvince= wx.getStorageSync('chooseProvince02');
       var chooseCity=wx.getStorageSync('chooseCity02');
     
      
        if (chooseProvince) {
          console.log('chooseCity')
          console.log(chooseCity)
          this.setData({
            city:wx.getStorageSync('chooseCity02')||'全国',
            item:wx.getStorageSync('chooseCity02')||'全国',
            province:wx.getStorageSync('chooseProvince01')||'全国'
          })
        }
       
       
       

       var type1= wx.getStorageSync('typel1');
       var type2= wx.getStorageSync('typel2');
       var type3= wx.getStorageSync('typel3');
       var type4= wx.getStorageSync('typel4');
       var type5= wx.getStorageSync('typel5');
       var type6= wx.getStorageSync('typel6');
       var type7= wx.getStorageSync('typel7');
       var type8= wx.getStorageSync('typel8');
       var type9= wx.getStorageSync('typel9');
       var type10= wx.getStorageSync('typel10');
       var type11= wx.getStorageSync('typel11');
      

      console.log("***********")
       console.log(type1)
       if (type1) {
          this.setData({
            type1:type1
          })
       }
        if (type2) {
          this.setData({
            type2:type2
          })
       }
        if (type3) {
          this.setData({
            type3:type3
          })
       }
        if (type4) {
          this.setData({
            type4:type4
          })
       }
        if (type5) {
          this.setData({
            type5:type5
          })
       }
        if (type6) {
          this.setData({
            type6:type6
          })
       }
        if (type7) {
          this.setData({
            type7:type7
          })
       }
        if (type8) {
          this.setData({
            type8:type8
          })
       }
        if (type9) {
          this.setData({
            type9:type9
          })
       }
        if (type10) {
          this.setData({
            type10:type10
          })
       }
        if (type11) {
          this.setData({
            type11:type11
          })
       }
       
      

    }, 
    onHide: function() {
    },
    textTitle:function (e) {
      console.log(e.detail.value)
       this.setData({
         title:e.detail.value
       })
    },
    //输入价格
    textPrice:function (e) {
       this.setData({
        price:e.detail.value
      })
    },
    textContentSeller:function (e) {
       console.log(e.detail.value)
       this.setData({
         sellerMsg:e.detail.value
       })
    },
    textContentButler:function (e) {
       console.log(e.detail.value)
       this.setData({
         butlerMsg:e.detail.value
       })
    },
    sendSheQu: function(){
      console.log(this.data.title)
       if (this.data.linshiArr.length==0) {
             wx.showToast({
              title: '请上传认证图片',
              icon:'none',
              duration: 2000
             })
            return
       }
        if (!this.data.price) {
             wx.showToast({
              title: '请输入价格',
              icon:'none',
              duration: 2000
             })
            return
       }
        if (!this.data.butlerMsg) {
             wx.showToast({
              title: '请输入管家描述',
              icon:'none',
              duration: 2000
             })
            return
       }
        if (!this.data.sellerMsg) {
             wx.showToast({
              title: '请输入卖家描述',
              icon:'none',
              duration: 2000
             })
            return
       }
       if (!this.data.type1||!this.data.type2||!this.data.type3||!this.data.type4||!this.data.type5||!this.data.type6||!this.data.type7||!this.data.type8||!this.data.type9||!this.data.type10||!this.data.type11) {
             wx.showToast({
              title: '请将检查认证信息输入完整',
              icon:'none',
              duration: 2000
             })
            return
       }
       console.log(this.data.linshiArr)
       if (this.data.changeId) {
        this.updataGoods();
        return
       }
       wx.request({
            url: getApp().globalData.baseUrl + '/wx/yxj/equipment/add',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                type:'10',
                price:this.data.price,
                butlerMsg:this.data.butlerMsg,
                sellerMsg:this.data.sellerMsg,
                url:this.data.linshiArr[0].url,
                isTestMachine:this.data.choosetype,
                province:this.data.province,
                city:this.data.city,
                "status":10,
                itemList:[this.data.type1,this.data.type2,this.data.type3,this.data.type4,this.data.type5,this.data.type6,this.data.type7,this.data.type8,this.data.type9,this.data.type10,this.data.type11]
            },
            header: this.data.header,
            success: function(res) {
               console.log(res)

               
             
                // wx.setStorageSync('chooseProvince4',null);
                // wx.setStorageSync('chooseCity4',null);
                wx.setStorageSync('typel1',null);
                wx.setStorageSync('typel2',null);
                wx.setStorageSync('typel3',null);
                wx.setStorageSync('typel4',null);
                wx.setStorageSync('typel5',null);
                wx.setStorageSync('typel6',null);
                wx.setStorageSync('typel7',null);
                wx.setStorageSync('typel8',null);
                wx.setStorageSync('typel9',null);
                wx.setStorageSync('typel10',null);
                wx.setStorageSync('typel11',null);
                 wx.showToast({
                  title: '发布成功，等待审核',
                  icon:'none',
                  duration: 2000
                })
                var timer=setTimeout(function(){
                  clearTimeout(timer)
                  wx.switchTab({
                  url: '../../pages/mine/mine'
                   })
                },2000)

                
                                    
            }
            })

    },
    updataGoods:function(){
     
      
       wx.request({
            url: getApp().globalData.baseUrl + '/wx/yxj/equipment/update',
            method:"POST",
            data:{
                id:this.data.changeId,
                openid:this.data.userInfo.openid,
                type:'10',
                price:this.data.price,
                butlerMsg:this.data.butlerMsg,
                sellerMsg:this.data.sellerMsg,
                url:this.data.linshiArr[0].url,
                isTestMachine:this.data.choosetype,
                province:this.data.province,
                city:this.data.city,
                "status":10,
                itemList:[this.data.type1,this.data.type2,this.data.type3,this.data.type4,this.data.type5,this.data.type6,this.data.type7,this.data.type8,this.data.type9,this.data.type10,this.data.type11]
            },
            header: this.data.header,
            success: function(res) {
               console.log(res)

                wx.setStorageSync('chooseProvince4',null);
                wx.setStorageSync('chooseCity4',null);
                wx.setStorageSync('typel1',null);
                wx.setStorageSync('typel2',null);
                wx.setStorageSync('typel3',null);
                wx.setStorageSync('typel4',null);
                wx.setStorageSync('typel5',null);
                wx.setStorageSync('typel6',null);
                wx.setStorageSync('typel7',null);
                wx.setStorageSync('typel8',null);
                wx.setStorageSync('typel9',null);
                wx.setStorageSync('typel10',null);
                wx.setStorageSync('typel11',null);
                
                wx.showToast({
                  title: '发布成功，等待审核',
                  icon:'none',
                  duration: 2000
                })
                var timer=setTimeout(function(){
                  clearTimeout(timer)
                  wx.switchTab({
                  url: '../../pages/mine/mine'
                   })
                },2000)
                                    
            }
            })
    },
    chooseImage:function () {
      var _self=this;
      wx.chooseImage({
        success (res) { 
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths[0])
          var imgUrl=tempFilePaths[0];
          console.log('##################')
          for (var i = tempFilePaths.length - 1; i >= 0; i--) {
              _self.uploadimg(tempFilePaths[i]);
              console.log(i)
          }
         
        }
      })
    },
    uploadimg:function (path) {
      var _self=this;
       wx.uploadFile({
             url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
            //url:getApp().globalData.baseUrl+'/imagesnode/uploadImg',
            filePath: path,
            header: {
              'content-type':'multipart/form-data',
              'openid':_self.data.userInfo.openid 
            },
            name: 'filePath',
            formData: {
              openid:_self.data.userInfo.openid 
            },
            success (res){
             var data = JSON.parse(res.data);
             console.log(data.obj);
              var arr=_self.data.linshiArr;
              var json={url:data.obj}
              arr.push(json)
              
                _self.setData({
                    linshiArr:arr
                })
              //do something
            }
          })
    },
    //选中设备类型
    chooseType:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            choosetype:e.target.dataset.code
        })
    },
    deleteImg:function (e) {
       console.log(e.target.dataset.code);
       var index=e.target.dataset.code;
       var arr=this.data.linshiArr;
       arr.splice(index,1);
       this.setData({
        linshiArr:arr
       })
    },
    todetail1:function(e){
      console.log(e.currentTarget.dataset.code)
       wx.navigateTo({
        url: '/pages/sendYanXuanDetail-'+e.currentTarget.dataset.code+'/sendYanXuan'
      })
    }

})